package textapp.controller;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import textapp.business.RegisterService;
import textapp.connector.ConnectorMessage;
import textapp.connector.MessageCode;
import textapp.connector.ServerConnectorMessage;
import textapp.dao.model.User;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * Servlet implementation class PollContactJsonServlet
 */
public class PollContactJsonServlet extends GenericJsonServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PollContactJsonServlet() {
        super();
        
    }

    private static final String PARAM_CONTACT = "contacts";
    
	@Override
	protected Object executeRequestJson(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String contactsJson = request.getParameter(PARAM_CONTACT);
		if (contactsJson == null) {
			return new ServerConnectorMessage(MessageCode.FAIL);
		}
		
		Gson gson = new Gson();
		Type strListType = new TypeToken<ArrayList<String>>(){}.getType();
		List<String> contactList = gson.fromJson(contactsJson, strListType);
		
		List<User> userList =  new RegisterService().pollContacts(contactList);
		
		return new ServerConnectorMessage(MessageCode.SUCCESS, userList);
	}



}
